package com.demo.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义一个异常处理解析类
 */
@Component
public class ExceptionHandlerResolver implements HandlerExceptionResolver {

    /**
     * 全局异常的处理
     * @param request
     * @param response
     * @param handler
     * @param e
     * @return
     */
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
        System.out.println("======resolver======");
        ModelAndView mv = new ModelAndView();

        if (e instanceof CustomerException) {
            mv.addObject("ex",e);
            mv.setViewName("cus_error");
        } else if (e instanceof Exception) {
            mv.addObject("ex",e);
            mv.setViewName("sys_error");
        } else {
            mv.addObject("ex",e);
            mv.setViewName("global_error");
        }

        return mv;
    }
}
